import java.io.IOException;
import java.util.InputMismatchException;
import java.util.Scanner;

public class Main {
    public static int getElement(int index,int[] array){
        if(array == null){
            throw new NullPointerException();
        }
        if(index >= array.length || index < 0){
            throw new ArrayIndexOutOfBoundsException("数组越界");
        }
        return array[index];
    }
    public static void main1(String[] args) {
        int[] array = new int[10];
        array = new int[]{1, 2, 3, 4};
        System.out.println(getElement(100, array));
    }

    public static void main2(String[] args) throws IOException {
        Config config = new Config();
        config.OpenConfig("config.ini");
    }
    public static void main3(String[] args) {
        try {
            int[] array = {1,2,3};
            System.out.println(array[3]); // 此处会抛出数组越界异常
        }catch (NullPointerException e){ // 捕获时候捕获的是空指针异常--真正的异常无法被捕获到
            e.printStackTrace();
        }
        System.out.println("后序代码");
    }
    public static void main4(String[] args) {
        int[] arr = {1, 2, 3};
        try {
            System.out.println("before");
// arr = null;
            System.out.println(arr[100]);
            System.out.println("after");
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("这是个数组下标越界异常");
            e.printStackTrace();
        } catch (NullPointerException e) {
            System.out.println("这是个空指针异常");
            e.printStackTrace();
        }
        System.out.println("after try catch");
    }
    public static void main5(String[] args) {
        int[] arr = {1, 2, 3};
        try {
            System.out.println("before");
            arr = null;
            System.out.println(arr[100]);
            System.out.println("after");
        } catch (NullPointerException e) {
            e.printStackTrace();
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println("after try catch");
    }

    public static void main6(String[] args) {
        int []arr = {1,2,3,4};
        System.out.println("aa");
        arr = null;
        System.out.println(arr[100]);
    }
    public static void main7(String[] args) {
        int[] arr = {1, 2, 3};
        try {
            System.out.println("before");
            arr = null;
            System.out.println(arr[100]);
            System.out.println("after");
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("after try catch");
    }
    public static void main8(String[] args) {
        try{
            int[] arr = {1,2,3};
            arr[100] = 10;
            arr[0] = 10;
        }catch (ArrayIndexOutOfBoundsException e){
            e.printStackTrace();
            System.out.println(e);
        }finally {
            System.out.println("finally中的代码一定会执行");
        }
        System.out.println("如果没有抛出异常，或者异常被处理了，try-catch后的代码也会执行");
    }

    public static int getData(){
            Scanner sc = null;
            try{
                sc = new Scanner(System.in);
                int data = sc.nextInt();
                return data;
            }catch (InputMismatchException e){
                e.printStackTrace();
            }finally {
                System.out.println("finally中代码");
            }
            System.out.println("try-catch-finally之后代码");
            if(null != sc){
                sc.close();
            }
            return 0;
        }

    public static void main9(String[] args) {
        int data = getData();
        System.out.println(data);
    }
    // 下面程序输出什么？
    public static void main10(String[] args) {
        System.out.println(func());
    }
    public static int func() {
        try {
            return 10;
        } finally {
            return 20;
        }
    }
    //A: 10 B: 20 C: 30 D: 编译失败
    public static void main(String[] args) {

            fun();

        System.out.println("after try catch");
    }
    public static void fun() {
        int[] arr = {1, 2, 3};
        System.out.println(arr[100]);
    }
}